package com.project.fiveCerealsSavoury.service.impl;

import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import com.project.fiveCerealsSavoury.domain.Order;
import com.project.fiveCerealsSavoury.domain.OrderState;
import com.project.fiveCerealsSavoury.domain.User;
import com.project.fiveCerealsSavoury.exception.BizException;
import com.project.fiveCerealsSavoury.service.OrderService;
import com.project.fiveCerealsSavoury.service.UserService;

@Service
public class Timing {
	 @Autowired
	 private OrderService orderService;
	 @Autowired
	 private UserService userService;
	 @Scheduled(cron = "0 0/1 * * * *")  
	 public void init() { 
	   System.out.println("启动");
	   try {
		   List<User> queryIsAdmin = userService.queryIsAdmin("否");
		   for (User user2 : queryIsAdmin) {
	    	   List<Order> queryStateOrder = orderService.queryStateOrder(user2.getId(),OrderState.未支付);
	    	   if (!queryStateOrder.isEmpty()) {
		    		try {
		    			Thread thread =new Thread();
		    			thread.start();
						Thread.sleep(1000*60*15);
						for (Order order : queryStateOrder) {
							orderService.removeOrder(order.getOrderNumber(), OrderState.未支付);
						}
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
	    	   }
		   }
		   return;
		} catch (BizException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	 }  
}
